//
//  ViewController.m
//  陀螺仪
//
//  Created by SongGang on 2017/3/23.
//  Copyright © 2017年 NK. All rights reserved.
//

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
// 陀螺仪
@property (nonatomic, strong) CMMotionManager *motionManager;
@property (weak, nonatomic) IBOutlet UILabel *gravityXLabel;
@property (weak, nonatomic) IBOutlet UILabel *gravityYLabel;
@property (weak, nonatomic) IBOutlet UILabel *gravityZLabel;
@property (weak, nonatomic) IBOutlet UILabel *accelerometerXLabel;
@property (weak, nonatomic) IBOutlet UILabel *accelerometerYLabel;
@property (weak, nonatomic) IBOutlet UILabel *accelerometerZLabel;
@property (weak, nonatomic) IBOutlet UILabel *stateLabel;
@property (weak, nonatomic) IBOutlet UILabel *jiasuduLabel;
@property (weak, nonatomic) IBOutlet UILabel *fanzhuanduLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUPCmmotion];
}

- (void)setUPCmmotion {

    
    
    // Y加速度大时手机处于竖立状态
    // Z加速度大时手机处于平放状态
    // X加速度大时手机处于横立状态
    __weak typeof(self) weakSelf = self;
    _motionManager = [[CMMotionManager alloc] init];
    _motionManager.deviceMotionUpdateInterval = 0.01;
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [_motionManager startDeviceMotionUpdatesToQueue:queue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
        
        double gravityX = motion.gravity.x;
        double gravityY = motion.gravity.y;
        double gravityZ = motion.gravity.z;
        
        double routeX = motion.attitude.pitch/M_PI*180.0;
        double routeY = motion.attitude.roll/M_PI*180.0;
        double routeZ= motion.attitude.yaw/M_PI*180.0;
        
//        double xyTheta = atan2(gravityX,gravityY)/M_PI*180.0;
//        double zTheta = atan2(gravityZ,sqrtf(gravityX*gravityX+gravityY*gravityY))/M_PI*180.0;

        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            weakSelf.gravityXLabel.text = [NSString stringWithFormat:@"X:  %.f",routeX];
            weakSelf.gravityYLabel.text = [NSString stringWithFormat:@"Y:  %.f",routeY];
            weakSelf.gravityZLabel.text = [NSString stringWithFormat:@"Z:  %.f",routeZ];
            weakSelf.accelerometerXLabel.text = [NSString stringWithFormat:@"X:  %.f",gravityX];
            weakSelf.accelerometerYLabel.text = [NSString stringWithFormat:@"Y:  %.f",gravityY];
            weakSelf.accelerometerZLabel.text = [NSString stringWithFormat:@"Z:  %.f",gravityZ];
            
            double maxNumber = [self returnMaxNumberWithOneNumber:gravityX withTwoNumber:gravityY withThreeNumber:gravityZ];
            if (fabs(gravityX) == maxNumber) {
                
                if (gravityX > 0) {
                    
                    if (routeY > 45 && routeY <= 89) {
                        
                        weakSelf.stateLabel.text = [NSString stringWithFormat:@"右横立,向正倾斜%.f度",90 - fabs(routeY)];
                        
                    }else if (routeY >= 91 && routeY < 135) {
                        
                        weakSelf.stateLabel.text = [NSString stringWithFormat:@"右横立,向反倾斜%.f度",fabs(routeY) - 90];
        
                    }else if (routeY > 89 && routeY < 91) {
                        
                         weakSelf.stateLabel.text = @"右横立";
                    }
                    
                    [weakSelf transFormWithLabel:-M_PI_2];
                    
                }else {
                    
                    if (routeY >= - 89) {
                        
                        weakSelf.stateLabel.text = [NSString stringWithFormat:@"左横立,向正倾斜%.f度",90 - fabs(routeY)];
                        
                    }else if (routeY <= -91 && routeY > -135) {
                        
                       weakSelf.stateLabel.text = [NSString stringWithFormat:@"左横立,向反倾斜%.f度",fabs(routeY) - 90];
                        
                    }else if (routeY > -91 && routeY < -89) {
                        
                        weakSelf.stateLabel.text = @"左横立";
                    }
                    [weakSelf transFormWithLabel: M_PI_2];
                }
                
            }else if (fabs(gravityY) == maxNumber) {
                
                if (gravityY > 0) {
                    
                    if (routeX < -45 && routeX >= -89 && gravityZ > 0) {
                        
                        weakSelf.stateLabel.text = [NSString stringWithFormat:@"倒竖立,向后倾斜%.f度",90 - fabs(routeX)];
                        
                    }else if (routeX < -45 && routeX >= -89 && gravityZ < 0) {
                        
                        weakSelf.stateLabel.text = [NSString stringWithFormat:@"倒竖立,向前倾斜%.f度",90 - fabs(routeX)];
                        
                    }else if (routeX >= -90 && routeX < -89) {
                        
                        weakSelf.stateLabel.text = @"倒竖立";
                        
                    }
                    [weakSelf transFormWithLabel: M_PI_2 * 2];

                }else {
                    
                    if (routeX > 45 && routeX <= 89 && gravityZ < 0) {
                        
                        weakSelf.stateLabel.text = [NSString stringWithFormat:@"正竖立,向后倾斜%.f度",90 - fabs(routeX)];
                        
                    }else if (routeX > 45 && routeX <= 89 && gravityZ > 0) {
                        
                        weakSelf.stateLabel.text = [NSString stringWithFormat:@"正竖立,向前倾斜%.f度",90 - fabs(routeX)];
                        
                    }else if (routeX > 89 && routeX <= 90) {
                        
                        weakSelf.stateLabel.text = @"正竖立";
                        
                    }
                    [weakSelf transFormWithLabel:0];
                }
                
            }else if (fabs(gravityZ) == maxNumber) {
                
                if (gravityZ > 0) {
                    
                    weakSelf.stateLabel.text = @"反平放";
                    
                }else {
                    
                    
                    if (fabs(routeY) > fabs(routeX)) {
                        
                        // 横
                        if (routeY >= -46 && routeY <= -1) {
                            
                            weakSelf.stateLabel.text = [NSString stringWithFormat:@"正平放,向左倾斜%.f度",fabs(routeY)];
                            
                        }else if (routeY >= 1) {
                            
                            weakSelf.stateLabel.text = [NSString stringWithFormat:@"正平放,向右倾斜%.f度",fabs(routeY)];
                            
                        }else if (routeY > -1 && routeY < 1) {
                            
                            weakSelf.stateLabel.text = @"正平放";
                            
                        }
                        
                    }else {
                    
                        // 竖
                        if (routeX >= -46 && routeX <= -1) {
                            
                            weakSelf.stateLabel.text = [NSString stringWithFormat:@"正平放,向下倾斜%.f度",fabs(routeX)];
                            
                        }else if (routeX >= 1) {
                            
                           weakSelf.stateLabel.text = [NSString stringWithFormat:@"正平放,向上倾斜%.f度",fabs(routeX)];
                            
                        }else if (routeX > -1 && routeX < 1) {
                            
                           weakSelf.stateLabel.text = @"正平放";
                        }
                    }
                }
                [weakSelf transFormWithLabel:0];
            }
            
        }];
        
    }];
    
    
//    [_motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
//        
//        weakSelf.gravityXLabel.text = [NSString stringWithFormat:@"X:  %.2f",gyroData.rotationRate.x];
//        weakSelf.gravityYLabel.text = [NSString stringWithFormat:@"Y:  %.2f",gyroData.rotationRate.y];
//        weakSelf.gravityZLabel.text = [NSString stringWithFormat:@"Z:  %.2f",gyroData.rotationRate.z];
//    }];
    
    // 加速度 手机顶端朝上，屏幕朝向自己。向左为X轴正值，向上为Y轴正值，朝向自己为Z轴正值
//    [_motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
    
//        weakSelf.accelerometerXLabel.text = [NSString stringWithFormat:@"X:  %.2f",motion.gravity.x];
//        weakSelf.accelerometerYLabel.text = [NSString stringWithFormat:@"Y:  %.2f",motion.gravity.y];
//        weakSelf.accelerometerZLabel.text = [NSString stringWithFormat:@"Z:  %.2f",motion.gravity.z];
        
//        double maxNumber = [self returnMaxNumberWithOneNumber:motion.gravity.x withTwoNumber:motion.gravity.y withThreeNumber:motion.gravity.z];
//        if (fabs(motion.gravity.x) == maxNumber) {
//            
//            if (motion.gravity.x > 0) {
//                NSLog(@"右横立");
//            }else {
//            
//                NSLog(@"左横立");
//            }
//            
//        }else if (fabs(motion.gravity.y) == maxNumber) {
//        
//            if (motion.gravity.y > 0) {
//                NSLog(@"倒竖立");
//            }else {
//                
//                NSLog(@"正竖立");
//            }
//            
//        }else if (fabs(motion.gravity.z) == maxNumber) {
//            
//            if (motion.gravity.z > 0) {
//                NSLog(@"反平放");
//            }else {
//                
//                NSLog(@"正平放");
//            }
//        }
//        
//    }];
    
}

- (void)setText:(NSString *)text {

    self.gravityXLabel.text = text;
    
}

- (double)returnMaxNumberWithOneNumber:(double)oneNumber withTwoNumber:(double)twoNumber withThreeNumber:(double)threeNumber {

   return MAX( MAX(fabs(oneNumber), fabs(twoNumber) ),fabs(threeNumber));
}

- (void)transFormWithLabel:(CGFloat)angle {

    self.gravityXLabel.transform = CGAffineTransformMakeRotation(angle);
    self.gravityYLabel.transform = CGAffineTransformMakeRotation(angle);
    self.gravityZLabel.transform = CGAffineTransformMakeRotation(angle);
    self.accelerometerXLabel.transform = CGAffineTransformMakeRotation(angle);
    self.accelerometerYLabel.transform = CGAffineTransformMakeRotation(angle);
    self.accelerometerZLabel.transform = CGAffineTransformMakeRotation(angle);
    self.stateLabel.transform = CGAffineTransformMakeRotation(angle);
    self.jiasuduLabel.transform = CGAffineTransformMakeRotation(angle);
    self.fanzhuanduLabel.transform = CGAffineTransformMakeRotation(angle);
    
}

@end
